{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Lonely Pixel II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findBlackPixel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #孤独像素 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个大小为 <code>m x n</code> 的二维字符数组 <code>picture</code> ，表示一张黑白图像，数组中的 <code>'B'</code> 表示黑色像素，<code>'W'</code> 表示白色像素。另给你一个整数 <code>target</code> ，请你找出并返回符合规则的 <strong>黑色</strong> 孤独像素的数量。</p>\n",
    "\n",
    "<p>黑色孤独像素是指位于某一特定位置 <code>(r, c)</code> 的字符 <code>'B'</code> ，其中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>行 <code>r</code> 和列 <code>c</code> 中的黑色像素恰好有 <code>target</code> 个。</li>\n",
    "\t<li>列 <code>c</code> 中所有黑色像素所在的行必须和行 <code>r</code> 完全相同。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://pic.leetcode.cn/1694957797-UWXAxl-image.png\" style=\"width: 493px; height: 333px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>picture = [[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"W\",\"B\",\"W\",\"B\",\"W\"]], target = 3\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>所有绿色的 'B' 都是我们所求的像素(第 1 列和第 3 列的所有 'B' )\n",
    "以行 r = 0 和列 c = 1 的 'B' 为例：\n",
    "- 规则 1 ，行 r = 0 和列 c = 1 都恰好有 target = 3 个黑色像素 \n",
    "- 规则 2 ，列 c = 1 的黑色像素分别位于行 0，行 1 和行 2。和行 r = 0 完全相同。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://pic.leetcode.cn/1694957806-FyCCMF-image.png\" style=\"width: 253px; height: 253px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>picture = [[\"W\",\"W\",\"B\"],[\"W\",\"W\",\"B\"],[\"W\",\"W\",\"B\"]], target = 1\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m ==&nbsp;picture.length</code></li>\n",
    "\t<li><code>n ==&nbsp;picture[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 200</code></li>\n",
    "\t<li><code>picture[i][j]</code> 为 <code>'W'</code> 或 <code>'B'</code></li>\n",
    "\t<li><code>1 &lt;= target &lt;= min(m, n)</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [lonely-pixel-ii](https://leetcode.cn/problems/lonely-pixel-ii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [lonely-pixel-ii](https://leetcode.cn/problems/lonely-pixel-ii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"],[\"W\",\"W\",\"B\",\"W\",\"B\",\"W\"]]\\n3', '[[\"W\",\"W\",\"B\"],[\"W\",\"W\",\"B\"],[\"W\",\"W\",\"B\"]]\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        ans = 0\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        rows = [0] * m\n",
    "        cols = [0] * n\n",
    "        col_row = [set() for _ in range(n)]\n",
    "\n",
    "        for i,row in enumerate(picture):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 'B':\n",
    "                    rows[i] +=1\n",
    "                    cols[j] += 1\n",
    "                    col_row[j].add(''.join(row))\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B' and rows[i] == cols[j] == target and len(col_row[j]) == 1:\n",
    "                    ans += 1\n",
    "                \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        m,n = len(picture),len(picture[0])\n",
    "        rows = [0]*m\n",
    "        cols = [0]*n\n",
    "        col_row = [set() for _ in range(n)]    #存储每一列包含的行元素\n",
    "        for i,row in enumerate(picture):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 'B':\n",
    "                    rows[i] +=1\n",
    "                    cols[j] += 1\n",
    "                    col_row[j].add(''.join(row))\n",
    "        return sum(1 for i in range(m) for j in range(n) if picture[i][j] == 'B' and rows[i] == cols[j] == target and len(col_row[j]) == 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        dic={}\n",
    "        ans=0\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row=[[] for _ in range(m)]\n",
    "        col=[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i].append(j)\n",
    "                    col[j]+=1\n",
    "            if len(row[i])==target:\n",
    "                dic[tuple(row[i])]=dic.get(tuple(row[i]),[])+[i]\n",
    "        for i in dic.keys():\n",
    "            if len(dic[i])==target:\n",
    "                for j in i:\n",
    "                    if col[j]==target:\n",
    "                        ans+=col[j]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        m,n = len(picture),len(picture[0])\n",
    "        rows = [0]*m\n",
    "        cols = [0]*n\n",
    "        col_row = [set() for _ in range(n)]    #存储每一列包含的行元素\n",
    "        for i,row in enumerate(picture):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 'B':\n",
    "                    rows[i] +=1\n",
    "                    cols[j] += 1\n",
    "                    col_row[j].add(''.join(row))\n",
    "        return sum(1 for i in range(m) for j in range(n) if picture[i][j] == 'B' and rows[i] == cols[j] == target and len(col_row[j]) == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        m,n = len(picture),len(picture[0])\n",
    "        rows = [0]*m\n",
    "        cols = [0]*n\n",
    "        hash_index = {}\n",
    "        col_row = [set() for _ in range(n)]    #存储每一列包含的行元素hash\n",
    "        for i,row in enumerate(picture):\n",
    "            # s = ''.join(row)\n",
    "            # if s not in hash_index:\n",
    "            #     hash_index[s] = i\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 'B':\n",
    "                    rows[i] +=1\n",
    "                    cols[j] += 1\n",
    "                    col_row[j].add(''.join(row))\n",
    "        return sum(1 for i in range(m) for j in range(n) if picture[i][j] == 'B' and rows[i] == cols[j] == target and len(col_row[j]) == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        row = [0 for _ in range(m)]\n",
    "        col = [0 for _ in range(n)]\n",
    "        col_record = collections.defaultdict(list)\n",
    "        row_record = collections.defaultdict(list)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "                    col_record[j].append(i)\n",
    "            row_record[''.join(picture[i])].append(i)\n",
    "        \n",
    "        count = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B' and row[i] == col[j] == target:\n",
    "                    if len(row_record[''.join(picture[i])]) == len(col_record[j]):\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        R = len(picture)\n",
    "        C = len(picture[0])\n",
    "\n",
    "        R_sum = [0]*R\n",
    "        C_sum = [0]*C\n",
    "\n",
    "        for i in range(R):\n",
    "            for j in range(C):\n",
    "                if picture[i][j] == 'B':\n",
    "                    R_sum[i] += 1\n",
    "                    C_sum[j] += 1\n",
    "        \n",
    "        same_dict = collections.defaultdict()\n",
    "        for i in range(R):\n",
    "            same_dict[''.join(picture[i])] = same_dict.get(''.join(picture[i]),0)+1\n",
    "        print(same_dict)\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(R):\n",
    "            for j in range(C):\n",
    "                if picture[i][j] == 'B'and R_sum[i]==target and C_sum[j]==target:\n",
    "                    if same_dict.get(''.join(picture[i]),0) == target:\n",
    "                        ans += 1\n",
    "        return ans\n",
    "        print(R_sum)\n",
    "        print(C_sum)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        rows, cols = [[] for _ in range(m)], [[] for _ in range(n)]\n",
    "        d = defaultdict(list)\n",
    "        for i, r in enumerate(picture):\n",
    "            d[''.join(r)].append(i)\n",
    "            for j, ch in enumerate(r):\n",
    "                if ch == 'B':\n",
    "                    rows[i].append(j)\n",
    "                    cols[j].append(i)\n",
    "        ans = 0\n",
    "        chosen_rows = []\n",
    "        for i in range(m):\n",
    "            if len(rows[i]) == target:\n",
    "                chosen_rows.append(i)\n",
    "        chosen_cols = []\n",
    "        for j in range(n):\n",
    "            if len(cols[j]) == target:\n",
    "                chosen_cols.append(j)\n",
    "        # final_cols = []\n",
    "        for j in chosen_cols:\n",
    "            col = cols[j]\n",
    "            s = ''.join(picture[col[0]])\n",
    "            if all(i in d[s] for i in col) and s.count('B') == target:\n",
    "                # final_cols.append(j)\n",
    "                ans += target\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        row=[[]for _ in range(len(picture))]\n",
    "        line=[[]for _ in range(len(picture[0]))]\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i].append(j)\n",
    "                    line[j].append(i)\n",
    "        ret=0\n",
    "        for j in range(len(line)):\n",
    "            if len(line[j])==target and len(row[line[j][0]])==target:\n",
    "                temp=row[line[j][0]]\n",
    "                judge=1\n",
    "                for i in line[j]:\n",
    "                    if row[i]!=temp:\n",
    "                        judge=0\n",
    "                        break\n",
    "                if judge==1:\n",
    "                    ret+=target\n",
    "        return ret\n",
    "[[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"]\n",
    ",[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"]\n",
    ",[\"W\",\"B\",\"W\",\"B\",\"B\",\"W\"]\n",
    ",[\"W\",\"W\",\"B\",\"W\",\"B\",\"W\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        # 完全相同 new ArrayList<>\n",
    "        n, m = len(picture), len(picture[0])\n",
    "        row = [[] for _ in range(n)]\n",
    "        col = [[] for _ in range(m)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i].append(j)\n",
    "                    col[j].append(i)\n",
    "\n",
    "        def check(arr, first):\n",
    "            for i, cur_row in enumerate(arr):\n",
    "                cur_row = arr[i]\n",
    "                if any(a != b for a, b in zip(picture[first], picture[cur_row])):\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        ans = 0\n",
    "        vis_row = [False] * n\n",
    "        vis_col = [False] * m\n",
    "        for i in range(n):\n",
    "            if vis_row[i]:\n",
    "                continue\n",
    "            for j in range(m):\n",
    "                if vis_col[j]:\n",
    "                    continue\n",
    "                if picture[i][j] == 'B' and len(row[i]) == target and len(col[j]) == target:\n",
    "                    vis_row[i] = True\n",
    "                    vis_col[j] = True\n",
    "                    # new ArrayList<>\n",
    "                    if check(col[j], i):\n",
    "                        ans += target\n",
    "                        \n",
    "        return ans\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        #先收集每一行，每一列B的个数\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        \n",
    "        row_list = {}\n",
    "        col_list = {}\n",
    "        for i in range(m):\n",
    "            col_tmp = []\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    col_tmp.append(j)\n",
    "                    if j in col_list:\n",
    "                        col_list[j].append(i)\n",
    "                    else:\n",
    "                        col_list[j] = [i]\n",
    "\n",
    "            row_list[i] = col_tmp\n",
    "\n",
    "        # count = 0\n",
    "        row_count = 0\n",
    "        col_count = 0\n",
    "        mark = True\n",
    "        while mark:\n",
    "            mark = False\n",
    "            for i in row_list:\n",
    "                if len(row_list[i]) != target:\n",
    "                    del row_list[i]\n",
    "                    mark = True\n",
    "                    break\n",
    "\n",
    "        mark = True\n",
    "        while mark:\n",
    "            mark = False\n",
    "            for i in col_list:\n",
    "                if len(col_list[i]) != target:\n",
    "                    del col_list[i]\n",
    "                    mark = True\n",
    "                    break\n",
    "        \n",
    "        print(row_list)\n",
    "        print(col_list)\n",
    "        #规则2\n",
    "        ans = 0\n",
    "        for i in col_list:\n",
    "            count = 0\n",
    "            for j in range(len(col_list[i])):\n",
    "                if col_list[i][j] in row_list and i in row_list[col_list[i][j]]:\n",
    "                    if j == 0:\n",
    "                        count += 1\n",
    "                    else:\n",
    "                        if picture[col_list[i][j]] == picture[col_list[i][0]]:\n",
    "                            count += 1\n",
    "            if count == target:\n",
    "                ans += count\n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "        #如示例1，有三个元素的r，去找对应的三个列是不是也有对应三个元素，再反过来找这三个元素对应的行是不是也也有对应的三个元素\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        #先收集每一行，每一列B的个数\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        \n",
    "        row_list = {}\n",
    "        col_list = {}\n",
    "        for i in range(m):\n",
    "            col_tmp = []\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    col_tmp.append(j)\n",
    "                    if j in col_list:\n",
    "                        col_list[j].append(i)\n",
    "                    else:\n",
    "                        col_list[j] = [i]\n",
    "\n",
    "            row_list[i] = col_tmp\n",
    "\n",
    "        # count = 0\n",
    "        row_count = 0\n",
    "        col_count = 0\n",
    "        import copy\n",
    "        row_list_ori = copy.deepcopy(row_list)\n",
    "        col_list_ori = copy.deepcopy(col_list)\n",
    "        mark = True\n",
    "        while mark:\n",
    "            mark = False\n",
    "            for i in row_list:\n",
    "                if len(row_list[i]) != target:\n",
    "                    del row_list[i]\n",
    "                    mark = True\n",
    "                    break\n",
    "\n",
    "        mark = True\n",
    "        while mark:\n",
    "            mark = False\n",
    "            for i in col_list:\n",
    "                if len(col_list[i]) != target:\n",
    "                    del col_list[i]\n",
    "                    mark = True\n",
    "                    break\n",
    "        \n",
    "        print(row_list)\n",
    "        print(col_list)\n",
    "        #规则2\n",
    "        ans = 0\n",
    "        for i in col_list:\n",
    "            count = 0\n",
    "            for j in range(len(col_list[i])):\n",
    "                if col_list[i][j] in row_list and i in row_list[col_list[i][j]]:\n",
    "                    if j == 0:\n",
    "                        count += 1\n",
    "                    else:\n",
    "                        if row_list_ori[col_list[i][j]] == row_list_ori[col_list[i][0]]:\n",
    "                            count += 1\n",
    "            if count == target:\n",
    "                ans += count\n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "        #如示例1，有三个元素的r，去找对应的三个列是不是也有对应三个元素，再反过来找这三个元素对应的行是不是也也有对应的三个元素\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        b=[]\n",
    "        dic={}\n",
    "        ans=0\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row=[[] for _ in range(m)]\n",
    "        # col=[[] for _ in range(n)]\n",
    "        col=[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i].append(j)\n",
    "                    # col[j].append(i)\n",
    "                    col[j]+=1\n",
    "                    b.append((i,j))\n",
    "            if len(row[i])==target:\n",
    "                dic[tuple(row[i])]=dic.get(tuple(row[i]),[])+[i]\n",
    "        # for x,y in b:\n",
    "        #     if len(row[x])==len(col[y])==target:\n",
    "        #         p=row[col[y][0]]\n",
    "        #         flag=1\n",
    "        #         for r in col[y][1:]:\n",
    "        #             if p!=row[r]:\n",
    "        #                 flag=0\n",
    "        #                 break\n",
    "        #         if flag==1:\n",
    "        #             ans+=1\n",
    "        for i in dic.keys():\n",
    "            if len(dic[i])==target:\n",
    "                for j in i:\n",
    "                    if col[j]==target:\n",
    "                        ans+=col[j]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findBlackPixel(self, picture: List[List[str]], target: int) -> int:\n",
    "        b=[]\n",
    "        ans=0\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row=[[] for _ in range(m)]\n",
    "        col=[[] for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i].append(j)\n",
    "                    col[j].append(i)\n",
    "                    b.append((i,j))\n",
    "        for x,y in b:\n",
    "            if len(row[x])==len(col[y])==target:\n",
    "                p=row[col[y][0]]\n",
    "                flag=1\n",
    "                for r in col[y][1:]:\n",
    "                    if p!=row[r]:\n",
    "                        flag=0\n",
    "                        break\n",
    "                if flag==1:\n",
    "                    ans+=1\n",
    "\n",
    "                \n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
